local assets =
{

    Asset("ANIM", "anim/yuemiheng_equipment.zip"),
    Asset("ATLAS", "images/inventoryimages/yuemiheng_spear.xml"), 
    Asset("IMAGE", "images/inventoryimages/yuemiheng_spear.tex"),
}

local prefabs =
{
    "yuemiheng_spear_fx",
}

local function SetFxOwner(inst, owner)
    if inst._fxowner ~= nil and inst._fxowner.components.colouradder ~= nil then
        inst._fxowner.components.colouradder:DetachChild(inst.fx)
    end
    inst._fxowner = owner
    if owner ~= nil then
        inst.fx.entity:SetParent(owner.entity)
        inst.fx.Follower:FollowSymbol(owner.GUID, "swap_object", nil, nil, nil, true)
        inst.fx.components.highlightchild:SetOwner(owner)
        if owner.components.colouradder ~= nil then
            owner.components.colouradder:AttachChild(inst.fx)
        end
    else
        inst.fx.entity:SetParent(inst.entity)
        --For floating
        inst.fx.Follower:FollowSymbol(inst.GUID, "swap_spear", nil, nil, nil, true)
        inst.fx.components.highlightchild:SetOwner(inst)
    end
end

local function PushIdleLoop(inst)
    if inst.components.finiteuses:GetUses() > 0 then
        inst.AnimState:PushAnimation("idle")
    end
end

local function OnStopFloating(inst)
    if inst.components.finiteuses:GetUses() > 0 then
        inst.fx.AnimState:SetFrame(0)
        inst:DoTaskInTime(0, PushIdleLoop) --#V2C: #HACK restore the looping anim, timing issues
    end
end

local function onequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_object")
    owner.AnimState:Show('ARM_carry')
    owner.AnimState:Hide('ARM_normal')


    SetFxOwner(inst, owner)
end

local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_object")
    owner.AnimState:Hide('ARM_carry')
    owner.AnimState:Show('ARM_normal')


    SetFxOwner(inst, nil)
end

local function onattack(inst,attacker,target)
    if YMHS:checkAlive(target) and not target:HasTag("structure") and not target:HasTag("wall") then
        target.components.health.maxhealth = math.max(1,target.components.health.maxhealth - TUNING.MOD_YUEMIHENG.SPEAR.DMG)
    end
end

local function SetupComponents(inst)
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(TUNING.MOD_YUEMIHENG.SPEAR.DMG)
    inst.components.weapon:SetRange(TUNING.MOD_YUEMIHENG.SPEAR.RANGE, TUNING.MOD_YUEMIHENG.SPEAR.RANGE)
    inst.components.weapon:SetOnAttack(onattack)
end

local function onfinished(inst)
    inst:Remove()
end

local function onsave(inst,data)
    data.ymh_spear_upgrade = inst.ymh_spear_upgrade
    data.is_ymh_spear_upgrade = inst.is_ymh_spear_upgrade

    data.ymh_staff_upgrade = inst.ymh_staff_upgrade
    data.is_ymh_staff_upgrade = inst.is_ymh_staff_upgrade

    data.ymh_spear_finiteuse = inst.ymh_spear_finiteuse
    data.ymh_staff_finiteuse = inst.ymh_staff_finiteuse
end
local function onpreload(inst,data)
    if data then
        inst.ymh_spear_upgrade = data.ymh_spear_upgrade
        inst.is_ymh_spear_upgrade = data.is_ymh_spear_upgrade

        inst.ymh_staff_upgrade = data.ymh_staff_upgrade
        inst.is_ymh_staff_upgrade = data.is_ymh_staff_upgrade

        inst.ymh_spear_finiteuse = data.ymh_spear_finiteuse
        inst.ymh_staff_finiteuse = data.ymh_staff_finiteuse

        if inst.is_ymh_spear_upgrade then
            if not inst:HasTag('is_ymh_spear_upgrade') then
                inst:AddTag('is_ymh_spear_upgrade')  
            end
            if inst.components.finiteuses then
                inst:RemoveComponent('finiteuses')
            end
        end

    end
end



local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("yuemiheng_spear")
    inst.AnimState:SetBuild("yuemiheng_equipment")
    inst.AnimState:PlayAnimation("idle", true)
    inst.AnimState:SetSymbolBloom("pb_energy_loop")
    inst.AnimState:SetSymbolBloom("stone")
    inst.AnimState:SetSymbolLightOverride("pb_energy_loop01", .5)
    inst.AnimState:SetSymbolLightOverride("pb_ray", .5)
    inst.AnimState:SetSymbolLightOverride("stone", .5)
    inst.AnimState:SetSymbolLightOverride("glow", .25)
    inst.AnimState:SetLightOverride(.1)

    inst:AddTag("rangedweapon")
    inst:AddTag("magicweapon")
    inst:AddTag("show_broken_ui")

    --weapon (from weapon component) added to pristine state for optimization
    inst:AddTag("weapon")

    inst:AddTag("yuemiheng_spear")

    inst.projectiledelay = FRAMES

    inst:AddComponent("floater")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.ymh_spear_upgrade = {
        ["redgem"] = false,
        ["bluegem"] = false,
        ["yellowgem"] = false,
        ["greengem"] = false,
        ["purplegem"] = false,
        ["orangegem"] = false,
    }
    inst.is_ymh_spear_upgrade = false

    local frame = math.random(inst.AnimState:GetCurrentAnimationNumFrames()) - 1
    inst.AnimState:SetFrame(frame)
    inst.fx = SpawnPrefab("yuemiheng_spear_fx")
    inst.fx.AnimState:SetFrame(frame)
    SetFxOwner(inst, nil)
    inst:ListenForEvent("floater_stopfloating", OnStopFloating)

    inst:AddComponent("inspectable") 

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/yuemiheng_spear.xml" 

    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")

    SetupComponents(inst)

    local setbonus = inst:AddComponent("setbonus")
    setbonus:SetSetName(EQUIPMENTSETNAMES.LUNARPLANT)

    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(TUNING.MOD_YUEMIHENG.SPEAR.FINITEUSE)
    inst.components.finiteuses:SetUses(TUNING.MOD_YUEMIHENG.SPEAR.FINITEUSE)
    inst.components.finiteuses:SetOnFinished(onfinished)


    inst.noplanarhitfx = true

    inst.OnSave = onsave
    inst.OnPreLoad = onpreload

    return inst
end

local function fxfn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddFollower()
    inst.entity:AddNetwork()

    inst:AddTag("FX")

    inst.AnimState:SetBank("yuemiheng_spear")
    inst.AnimState:SetBuild("yuemiheng_equipment")
    inst.AnimState:PlayAnimation("swap_loop", true)
    inst.AnimState:SetSymbolBloom("pb_energy_loop")
    inst.AnimState:SetSymbolBloom("stone")
    inst.AnimState:SetSymbolLightOverride("pb_energy_loop01", .5)
    inst.AnimState:SetSymbolLightOverride("pb_ray", .5)
    inst.AnimState:SetSymbolLightOverride("stone", .5)
    inst.AnimState:SetSymbolLightOverride("glow", .25)
    inst.AnimState:SetLightOverride(.1)

    inst:AddComponent("highlightchild")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("colouradder")

    inst.persists = false

    return inst
end

return Prefab("yuemiheng_spear", fn, assets, prefabs),
    Prefab("yuemiheng_spear_fx", fxfn, assets)